package com.hrm.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.hrm.pojo.Notice;
import com.hrm.service.NoticeService;
import com.hrm.utils.JwtUtil;
import com.hrm.utils.entity.PageResult;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 公告管理
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "公告管理模块接口")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;


    @ApiOperation(value = "分页模糊查询公告分页显示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页数",required = true,paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType = "query",dataType = "int"),
    })
    @PostMapping("/list")
    public Result findNoticeList(Integer pageNum, Integer pageSize , @RequestBody Notice notice)throws Exception{
       List<Notice> noticeList=noticeService.findByTitleAndContent(pageNum,pageSize,notice);
        if(noticeList==null ||noticeList.isEmpty()){
            return new Result(false,StatusCode.ERROR,"获取公告分页数据失败");
        }
        PageInfo<Notice> pageInfo = new PageInfo<>(noticeList, 10);
        return  new Result(true, StatusCode.SUCCESS,"获取公告列表数据成功",pageInfo);
    }

    //增加公告
    @PostMapping("/add")
    @ApiOperation(value = "添加公告")
    public Result addNotice(@RequestBody Notice notice)throws Exception{
        boolean flag=noticeService.addNotice(notice);
        return new Result(flag,flag==true? StatusCode.SUCCESS:StatusCode.ERROR,flag==true? "添加成功":"添加失败");
    }

    //删除公告
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除公告")
    public Result deleteNotice(@RequestBody ArrayList<Integer> noticeIdList)throws Exception{
        boolean flag=noticeService.deleteNotice(noticeIdList);
        return new Result(flag,flag==true? StatusCode.SUCCESS:StatusCode.ERROR,"删除成功");
    }

    //修改公告
    @PutMapping("/update")
    @ApiOperation(value = "修改公告")
    public Result updateNotice(@RequestBody Notice notice)throws Exception{
        boolean flag=noticeService.updateNotice(notice);
        return new Result(flag,flag==true? StatusCode.SUCCESS:StatusCode.ERROR,"修改成功");
    }

    //预览公告
    @GetMapping("/show/{id}")
    @ApiOperation(value ="预览公告")
    public Result prviewNotice(@PathVariable Integer id)throws Exception{
        Notice prviewNotice=noticeService.prviewNotice(id);
        if(prviewNotice==null){
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
        return new Result(true, StatusCode.SUCCESS,"查询成功",prviewNotice);
    }
}
